package com.itheima.controller.returnreason;

import com.itheima.api.returnreason.OmsOrderReturnReasonService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.pojo.OmsOrderReturnReason;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/returnReason")
@Slf4j
public class OmsOrderReturnReasonController {

    @DubboReference
    private OmsOrderReturnReasonService reasonService;

    /**
     * 分页查询退货原因
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(Integer pageNum, Integer pageSize) {
        Map map = reasonService.limit(pageNum, pageSize);
        return AjaxResult.success(map);
    }

    /**
     * 添加退货原因
     *
     * @param reason
     * @return
     */
    @PostMapping("/create")
    public AjaxResult save(@RequestBody OmsOrderReturnReason reason) {
        // 1.设置添加时间
        reason.setCreateTime(LocalDateTime.now());
        //2.添加
        reasonService.save(reason);

        return AjaxResult.success();
    }

    /**
     * 批量删除退货原因
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam List<Integer> ids) {
        reasonService.deleteByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 修改退货原因启用状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/update/status")
    public AjaxResult updateStatus(Integer status,@RequestParam List<Integer> ids) {
        reasonService.updateStatus(status,ids);
        return AjaxResult.success();
    }

    /**
     * 修改退货原因
     * @param id
     * @param reason
     * @return
     */
    @PostMapping("/update/{id}")
    public AjaxResult updateReason(@PathVariable Integer id, @RequestBody OmsOrderReturnReason reason) {
        reasonService.updateReason(id,reason);
        return AjaxResult.success();
    }

    /**
     * 获取单个退货原因详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getOne(@PathVariable Integer id){
        return AjaxResult.success(reasonService.getById(id));
    }
}
